<template>
  <!--回收案件池-->
  <section class="page system-parameter-manage">
    <data-form :model="queryModel" @handleResetForm="follFeedback" @onSearch="refreshData" autoSearch>
      <!--输入区域-->
      <template slot="inputs">
        <el-form-item label="批次号" prop="outBatch">
          <el-input v-model="queryModel.outBatch"></el-input>
        </el-form-item>
        <el-form-item label="订单编号" prop="caseNumber">
          <el-input v-model="queryModel.caseNumber"></el-input>
        </el-form-item>
        <el-form-item label="客户姓名" prop="personalName">
          <el-input v-model="queryModel.personalName"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="mobileNo">
          <el-input v-model="queryModel.mobileNo"></el-input>
        </el-form-item>
        <el-form-item label="身份证号" prop="idCard">
          <el-input v-model="queryModel.idCard"></el-input>
        </el-form-item>
        <el-form-item label="地区" prop="area">
          <el-input v-model="queryModel.area"></el-input>
        </el-form-item>
        <el-form-item label="催收员" prop="currentCollector">
          <el-input v-model="queryModel.currentCollector"></el-input>
        </el-form-item>
        <el-form-item label="案件金额" prop="caseMoney">
          <number-range :isInteger="true" v-model="queryModel.caseMoney"></number-range>
        </el-form-item>
        <el-form-item label="欠款金额" prop="outAmount">
          <number-range :isInteger="true" v-model="queryModel.outAmount"></number-range>
        </el-form-item>
        <el-form-item label="逾期期数" prop="overduePeriod">
          <el-select clearable placeholder="请选择" v-model="queryModel.overduePeriod">
            <el-option :key="index" :label="name" :value="code.replace(/M|\+/, '')" v-for="{index,name,code} in $dict.getDictData('OverduePeriodStatus')"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="逾期天数" prop="overdueDays">
          <number-range v-model="queryModel.overdueDays"></number-range>
        </el-form-item>
        <el-form-item label="行动代码" prop="followupBack">
          <el-select clearable placeholder="所有" v-model="queryModel.followupBack">
            <el-option :key="index" :label="name" :value="code" v-for="{index,name,code} in $dict.getDictData('FollowupBack')"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="流转来源" prop="turnSource">
          <el-select clearable placeholder="所有" v-model="queryModel.turnSource">
            <el-option :key="index" :label="name" :value="code" v-for="{index,name,code} in $dict.getDictData('TurnSource')"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="产品类型" prop="productCategory">
          <el-input v-model="queryModel.productCategory"></el-input>
        </el-form-item>
        <el-form-item label="产品名称" prop="productName">
          <el-input v-model="queryModel.productName"></el-input>
        </el-form-item>
        <el-form-item label="案件流入时间" prop="followInTime">
          <el-date-picker :editable="false" clearable placeholder="选择日期时间" type="daterange" v-model="queryModel.followInTime"></el-date-picker>
        </el-form-item>
        <el-form-item label="案件标签" prop="labelName">
          <get-label-name :formModel="queryModel" v-model="queryModel.labelName"></get-label-name>
        </el-form-item>
      </template>
    </data-form>
    <data-box :data="dataList" :page="pageService" :selectionList.sync="selectionList" @onPageChange="refreshData">
      <template slot="buttons">
        <el-button @click="moveIntoAllocated" v-auth="1018">移入委外待分配</el-button>
        <el-button @click="applyTurn" v-auth="1019">申请流转</el-button>
      </template>
      <template slot="columns">
        <!--数据列区域-->
        <el-table-column :min-width="$helper.getColumnWidth(6)" label="订单编号" prop="caseNumber">
          <template slot-scope="scope">
            <el-button @click="openExecutionPage(scope.row)" type="text">{{scope.row.caseNumber}}</el-button>
          </template>
        </el-table-column>
        <el-table-column :min-width="$helper.getColumnWidth(4)" label="批次号" prop="outBatch"></el-table-column>
        <el-table-column :min-width="$helper.getColumnWidth(4)" label="受托方" prop="outsName"></el-table-column>
        <el-table-column :min-width="$helper.getColumnWidth(5)" label="客户姓名" prop="personalName"></el-table-column>
        <el-table-column :min-width="$helper.getColumnWidth(4)" label="手机号" prop="mobileNo"></el-table-column>
        <el-table-column :min-width="$helper.getColumnWidth(6)" label="身份证号" prop="idCard"></el-table-column>
        <el-table-column :min-width="$helper.getColumnWidth(3)" label="地区" prop="area" show-overflow-tooltip></el-table-column>
        <el-table-column :min-width="$helper.getColumnWidth(4)" label="案件金额(元)" prop="caseMoney"></el-table-column>
        <el-table-column :min-width="$helper.getColumnWidth(4)" label="欠款金额(元)" prop="overdueAmount"></el-table-column>
        <el-table-column :formatter="row => $common.isNotValue(row.overduePeriod) ? '':`M${row.overduePeriod}`" :min-width="$helper.getColumnWidth(3)" label="逾期期数" prop="overduePeriod"></el-table-column>
        <el-table-column :min-width="$helper.getColumnWidth(3)" label="逾期天数" prop="overdueDays"></el-table-column>
        <el-table-column :min-width="$helper.getColumnWidth(3)" label="产品类型" prop="productCategory"></el-table-column>
        <el-table-column :min-width="$helper.getColumnWidth(3)" label="产品名称" prop="productName"></el-table-column>
        <el-table-column :formatter="(row) => $filter.dictConvert(row.turnSource,'TurnSource')" :min-width="$helper.getColumnWidth(3)" label="流转来源" prop="turnSource"></el-table-column>
        <el-table-column :formatter="(row) => $filter.dateTimeFormat(row.followInTime)" :min-width="$helper.getColumnWidth(5)" label="案件流入时间" prop="followInTime"></el-table-column>
        <el-table-column :formatter="(row) => $filter.dateTimeFormat(row.caseAllocationTime)" :min-width="$helper.getColumnWidth(5)" label="案件分配时间" prop="caseAllocationTime"></el-table-column>
        <el-table-column :formatter="(row) => $filter.dateTimeFormat(row.followupTime)" :min-width="$helper.getColumnWidth(5)" label="跟进时间" prop="followupTime"></el-table-column>
        <el-table-column :formatter="(row) => $filter.dictConvert(row.followupBack,'FollowupBack')" :min-width="$helper.getColumnWidth(4)" label="行动代码" prop="followupBack"></el-table-column>
        <el-table-column :formatter="(row) => $filter.dictConvert(row.lawerStatus,'LawerStatus')" :min-width="$helper.getColumnWidth(3)" label="诉讼状态" prop="lawerStatus"></el-table-column>
        <el-table-column :min-width="$helper.getColumnWidth(4)" label="反欺诈结果" prop="unCheatResult"></el-table-column>
        <el-table-column :min-width="$helper.getColumnWidth(10)" label="案件标签" prop="labelName"></el-table-column>
      </template>
    </data-box>

    <!--弹框-申请流转-->
    <el-dialog :close-on-click-modal="false" :show-close="false" :visible.sync="dialog.applyFlow" @close="dialog.applyFlow = false" title="流转申请">
      <apply-flow :fromChannel="caseChannel" :selectionList="selectionList" @close="dialog.applyFlow=false" @refreshList="refreshData" ref="applyFlow"></apply-flow>
    </el-dialog>
  </section>
</template>

<script lang="ts">
import Vue from "vue";
import Component from "vue-class-component";
import { Dependencies, Layout, Auth } from "~/core/decorator";

import NumberRange from "~/components/common/number-range.vue";
import ApplyFlow from "~/components/outsource-manage/recycle-manage/apply-flow.vue"; // 案件分配
import { PageService } from "~/utils/page.service";
import { SortService } from "~/utils/sort.service";
import { OutsourceRecycleService } from "~/services/domain-service/outsource-recycle-manage.service";
import { CityService } from "~/utils/city.service";
import { CASE_CHANNEL, MENU_CHANNEL } from "~/config/enum.config";
import { namespace } from "vuex-class";
import GetLabelName from "~/components/labeling-strategy/get-label-name.vue";

@Auth(1006)
@Layout("workspace")
@Component({
  components: {
    GetLabelName,
    NumberRange,
    ApplyFlow
  }
})
export default class RecycleManageIndex extends Vue {
  @Dependencies(OutsourceRecycleService)
  private outsourceRecycleService: OutsourceRecycleService;
  @Dependencies(PageService)
  private pageService: PageService;
  @Dependencies(SortService)
  private sortService: SortService;

  private caseChannel = CASE_CHANNEL.OUTER;

  private dialog: any = {
    applyFlow: false
  };
  private proinviceList: any[] = []; // 省
  private cityList: any[] = []; //市

  private queryModel: any = {
    outBatch: "",
    caseNumber: "",
    personalName: "",
    mobileNo: "",
    idCard: "",
    localAddress: "",
    currentCollector: "",
    overduePeriod: "",
    turnSource: "",
    followupBack: "",
    productCategory: "",
    productName: "",
    followInTime: [],
    area: "",
    outAmount: {
      max: "",
      min: ""
    },
    caseMoney: { min: "", max: "" },
    overdueDays: {
      max: "",
      min: ""
    },
    labelName: []
  };
  private dataList: any[] = []; //数据列表
  private selectionList: any[] = []; //选中数据

  private mounted() {
    this.refreshData();
    this.getProvinces();
  }

  //获取省数据
  private getProvinces() {
    this.proinviceList = CityService.getCityData({ level: 1 });
  }

  //获取市
  private getCitys(provinceId) {
    if (this.queryModel.cityCode) {
      this.queryModel.cityCode = "";
    }
    this.cityList = this.$city.getCityData({ level: 1, id: provinceId }); // 城市列表
  }

  /**
   * 移入委外待分配
   */
  private moveIntoAllocated() {
    if (this.selectionList.length === 0) {
      this.$message.info("请选择需要移入委外待分配的案件");
      return;
    }

    const temp: any[] = [];
    this.selectionList.map(value => {
      temp.push(value.id);
    });
    const query = {
      caseIds: temp
    };
    //移入委外待分配
    this.outsourceRecycleService.caseMoveAllocation(query).subscribe(data => {
      this.refreshData();
      this.$message.success("移入待分配案件成功!");
    });
  }

  /**
   * 申请流转
   */
  private applyTurn() {
    if (this.selectionList.length === 0) {
      this.$message.info("请选择需要移入委外待分配的案件");
      return;
    }
    this.dialog.applyFlow = true;
  }

  private refreshData() {
    //默认加载列表
    this.outsourceRecycleService
      .caseOutRecoveryList(this.queryModel, this.pageService, this.sortService)
      .subscribe(data => {
        this.dataList = data;
        this.selectionList = [];
      });
  }

  /**
   * 进入查看执行页
   */
  private openExecutionPage(row) {
    this.$router.openCaseDetail(row.id, {
      menuChannel: MENU_CHANNEL.Outsource_recover,
      hiddenCaseList: true,
      hiddenAction: true
    });
  }

  /**
   * 重置numberRange
   */
  private follFeedback() {
    this.queryModel.outAmount = { min: "", max: "" };
    this.queryModel.overdueDays = { min: "", max: "" };
    this.queryModel.proinviceCode = "";
    this.queryModel.cityCode = "";
    this.queryModel.caseMoney = { min: "", max: "" };
  }
}
</script>

<style lang="less">
</style>
